package com.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.product.enmu.HttpStatusEnum;
import com.product.entity.OrderItem;
import com.product.handler.ParameterValidationException;
import com.product.result.ApiResult;
import com.product.service.IAssemblyLineService;
import com.product.service.IOrderItemService;
import com.product.to.AddAssemblyLineTo;
import com.product.to.UpdateAssemblyLineTo;
import com.product.utils.PageUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "流水线控制器")
@RestController
@RequestMapping("/assemblyline")
public class AssemblyLineController {
    @Autowired
    private IAssemblyLineService assemblyLineService;
    @Autowired
    private IOrderItemService orderItemService;

    @Parameters({
            @Parameter(name = "size", description = "每页条数，默认10条"),
            @Parameter(name = "current", description = "分页页数，默认第一页"),
            @Parameter(name = "lineName", description = "流水线名称"),
            @Parameter(name = "orderItemId", description = "订单项id"),
            @Parameter(name = "timeOrderBy", description = "按照时间顺序排序,0->升序,1->降序"),
            @Parameter(name = "token", description = "用户token,放到请求头的Authorization属性")
    })
    @Operation(summary = "获取所有流水线")
    @GetMapping("/all")
    private ApiResult<PageUtils> getAllAssemblyLines(@RequestParam(name = "size", defaultValue = "10") Integer size,
                                             @RequestParam(name = "current", defaultValue = "1") Integer current,
                                             @RequestParam(name = "lineName", required = false) String lineName,
                                             @RequestParam(name = "orderItemId", required = false) Integer orderItemId,
                                             @RequestParam(name = "timeOrderBy", defaultValue = "1") Integer timeOrderBy) {
        return ApiResult.ok(assemblyLineService.getAllAssemblyLine(size, current, lineName, orderItemId, timeOrderBy));
    }

    @Parameter(name = "token", description = "用户token,放到请求头的Authorization属性")
    @Operation(summary = "添加流水线")
    @PostMapping("/add")
    private ApiResult<Boolean> addAssemblyLines(@Valid @RequestBody AddAssemblyLineTo assemblyLine, BindingResult result) throws ParameterValidationException {
        if (result.hasErrors()) {
            StringBuilder errors = new StringBuilder();
            result.getAllErrors().forEach(error -> errors.append(error.getDefaultMessage()).append("\n"));
            throw new ParameterValidationException(errors.toString(), HttpStatusEnum.PARAM_ERROR.getCode());
        }

        if (orderItemService.list(new LambdaQueryWrapper<OrderItem>().eq(OrderItem::getOrderItemId, assemblyLine.getOrderItemId())).isEmpty()&&assemblyLine.getOrderItemId()!=null) {
            return ApiResult.getResult(null, HttpStatusEnum.ERROR.getCode(), "订单项不存在");
        }
        assemblyLineService.addAssemblyLine(assemblyLine);
        return ApiResult.ok();
    }

    @Parameter(name = "token", description = "用户token,放到请求头的Authorization属性")
    @Operation(summary = "修改流水线")
    @PostMapping("/update")
    private ApiResult<Boolean> updateAssemblyLine(@Valid @RequestBody UpdateAssemblyLineTo assemblyLine, BindingResult result) throws ParameterValidationException {
        if (result.hasErrors()) {
            StringBuilder errors = new StringBuilder();
            result.getAllErrors().forEach(error -> errors.append(error.getDefaultMessage()).append("\n"));
            throw new ParameterValidationException(errors.toString(), HttpStatusEnum.PARAM_ERROR.getCode());
        }
        if (orderItemService.list(new LambdaQueryWrapper<OrderItem>().eq(OrderItem::getOrderItemId, assemblyLine.getOrderItemId())).isEmpty()) {
            return ApiResult.getResult(null, HttpStatusEnum.ERROR.getCode(), "订单项不存在");
        }
        assemblyLineService.updateAssemblyLine(assemblyLine);
        return ApiResult.ok();
    }

    @Parameters({
            @Parameter(name = "assemblyLineId", description = "流水线id"),
            @Parameter(name = "token", description = "用户token,放到请求头的Authorization属性")
    })
    @Operation(summary = "删除流水线")
    @PostMapping("/delete")
    private ApiResult<Boolean> deleteAssemblyLine(@RequestParam(name = "assemblyLineId") Integer assemblyLineId) {
        assemblyLineService.deleteAssemblyLine(assemblyLineId);
        return ApiResult.ok();
    }

    @Parameter(name = "token", description = "用户token,放到请求头的Authorization属性")
    @Operation(summary = "批量删除流水线")
    @PostMapping("/deletes")
    private ApiResult<Boolean> deleteAssemblyLines(@RequestBody List<Integer> assemblyLineIds) {
        assemblyLineService.deleteAssemblyLines(assemblyLineIds);
        return ApiResult.ok();
    }
}
